package cn.gson.springboot.controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gson.springboot.model.pojos.Role;
import cn.gson.springboot.model.service.GrantService;
import cn.gson.springboot.model.vo.FunctionVo;

@Controller
public class GrantController {
	@Autowired
	private GrantService gs;

	/**
	 * 权限管理--角色管理
	 */
	
	//角色页面
	@RequestMapping("role-manager")
	public void roleManager() {
	}
	
	//查询角色
	@RequestMapping("role-list")
	@ResponseBody
	public List<Role> roleList(){
		
		return gs.roleList();
	}
	//处理一级二级功能
	@RequestMapping("init-grant")
	@ResponseBody
	public List<FunctionVo> initGrant(int roleId){
		return gs.grantList(roleId);
	}
	
	/**
	 * 授权
	 * @param roleId
	 * @param funIds
	 */
	@RequestMapping("sava-grant")
	@ResponseBody
	public void saveGrant(int roleId,String funIds) {
		List<Integer> ids = new ArrayList<>();
		//funIds---[5,6,7]
		StringBuffer sb = new StringBuffer(funIds);
		sb.deleteCharAt(sb.length()-1);
		sb.deleteCharAt(0);
		//切割5,6,7
		for (String s : sb.toString().split(",")) {
			ids.add(Integer.parseInt(s));
		}
		gs.saveGrant(roleId,ids);
	}
}
